<?php

       //define('WP_USE_THEMES', true); //$blog='NULL'; $blogdata='NULL';
        //$optiondomain = 'system';
	//PEAK
        $_REQUEST['b']='system';
	require_once('../../../private.php');
	require_once(CONFIGPATH.'/wp-config.php');

	/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	*/
$k_id = $wpdb->escape($_GET['id']);
$k_action = $wpdb->escape($_GET['action']);
$k_path = $wpdb->escape($_GET['path']);
$table_name = $wpdb->prefix . 'ck_karma';

if($k_id && $k_action && $k_path) {
    //Check to see if the comment id exists and grab the rating
    $query = "SELECT * FROM `$table_name` WHERE ck_comment_id = $k_id";
    $result = mysql_query($query);


	if(!$result) { die('error|mysql: '.mysql_error()); }
	
   if(mysql_num_rows($result))
	{
      if($row = @mysql_fetch_assoc($result))
      {
			if(strstr($row['ck_ips'], getenv("REMOTE_ADDR"))) {
            die('error|A vote has already been registered from this IP address'); }
         $ck_ips = $row['ck_ips'] . ',' . getenv("REMOTE_ADDR"); // IPs are separated by ','
      }
		
      if($k_action == 'add') {
         $rating = $row['ck_rating_up'] + 1;
         $direction = 'up';
      }
      elseif($k_action == 'subtract')
      {
         $rating = $row['ck_rating_down'] + 1;
         $direction = 'down';
      } else {
            die('error|Try again later'); //No action given.
      }
		
      $query = "UPDATE `$table_name` SET ck_rating_$direction = '$rating', ck_ips = '" . $ck_ips  . "' WHERE ck_comment_id = $k_id";
      $result = mysql_query($query); 
      if(!$result)
		{
	 //die('error|query '.$query);
         die('error|Query error');
      }
        
		if(!mysql_affected_rows())
		{
         die('error|affected '. $rating);
      }
   } else {
        die('error|Comment doesnt exist'); //Comment id not found in db, something wrong ?
   }
} else {
    die('error|Fatal: html format error');
}

//This sends the data back to the js to process and show on the page
echo('done|'. $k_id .'|'. $rating .'|'. $k_path .'|'. $direction);
?>
